package conexion;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public class Vender {

	public static void actionHouse(String itemId){
		URL urlBuyUser;
		HttpsURLConnection connectionBuyUser = null;
		String lineBuyUser="0";
		String setCookie = Conexion.EASW_KEY + "; " + Conexion.easf_sess_com + "; FUTWebPhishing" + Conexion.FUTWebPhishing;
		System.out.println("ItemId: "+itemId);
		String data="{\"duration\":3600,\"startingBid\":400,\"itemData\":{\"id\":"+itemId+"},\"buyNowPrice\":450}";
		        
		try{
			urlBuyUser = new URL("https://utas.fut.ea.com/ut/game/fifa13/auctionhouse");
			connectionBuyUser = (HttpsURLConnection)urlBuyUser.openConnection();
			connectionBuyUser.setRequestMethod("POST");
			connectionBuyUser.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
			connectionBuyUser.setRequestProperty("Cookie", setCookie);
			connectionBuyUser.setRequestProperty("Content-Type", "application/json");       
			connectionBuyUser.setRequestProperty("X-UT-SID", Conexion.XUTSID);       
			connectionBuyUser.setRequestProperty("X-HTTP-Method-Override", "POST");
			connectionBuyUser.setUseCaches (false);connectionBuyUser.setDoInput(true);connectionBuyUser.setDoOutput(true);          
			
			//Send request
			DataOutputStream wrBuyUser = new DataOutputStream (connectionBuyUser.getOutputStream ());
			wrBuyUser.writeBytes (data);
			wrBuyUser.flush ();
			wrBuyUser.close ();
			                         
			//Get Response  
			InputStream isBuyUser = connectionBuyUser.getInputStream();             
			BufferedReader rdBuyUser = new BufferedReader(new InputStreamReader(isBuyUser));                
			lineBuyUser = rdBuyUser.readLine();             
			System.out.println("\n"+lineBuyUser);
			System.out.println("Colocado en venta");
			Thread.sleep(500);
			//listaCompra();
		}catch(Exception e){}
	}
	    
	public static void tradePile(){
		System.out.println("Trade pile");
		URL urlTradePile;
		HttpsURLConnection connectionTradePile = null;
		String lineTradePile="0";
		String setCookie = Conexion.EASW_KEY + "; " + Conexion.easf_sess_com + "; FUTWebPhishing" + Conexion.FUTWebPhishing;
		try{
			urlTradePile = new URL("https://utas.fut.ea.com/ut/game/fifa13/tradepile");
			connectionTradePile = (HttpsURLConnection)urlTradePile.openConnection();
			connectionTradePile.setRequestMethod("POST");
			connectionTradePile.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
			connectionTradePile.setRequestProperty("Cookie", setCookie);
			connectionTradePile.setRequestProperty("Content-Type", "application/json");       
			connectionTradePile.setRequestProperty("X-UT-SID", Conexion.XUTSID);       
			connectionTradePile.setRequestProperty("X-HTTP-Method-Override", "GET"); 
			connectionTradePile.setUseCaches (false);connectionTradePile.setDoInput(true);connectionTradePile.setDoOutput(true);          
			                                      
			//Get Response  
			InputStream isTradePile = connectionTradePile.getInputStream();             
			BufferedReader rdTradePile = new BufferedReader(new InputStreamReader(isTradePile));                
			lineTradePile = rdTradePile.readLine();
			String lineTradePile2 = lineTradePile;
			System.out.println("\n"+lineTradePile);
			int contador=0;
			System.out.println("contador");
			
			while (lineTradePile.indexOf("itemData") > -1) {
				lineTradePile = lineTradePile.substring(lineTradePile.indexOf("itemData")+"itemData".length(),lineTradePile.length());
				contador++; 
			}
			        	    
			System.out.println("Items en la pila: "+contador);

			for(int x=0;x<contador;x++){
			int currentBid= JsonParse.currentBid(lineTradePile2, x);
				if(currentBid>1){
					String tradeId = JsonParse.tradeId(lineTradePile2, x);
					System.out.println("Items a eliminar"+tradeId);
					Eliminar.eliminarContrato(tradeId);
				}
			}
			
			for(int x=0;x<contador;x++){             		
				int expires = JsonParse.expires(lineTradePile2, x);
				if(expires==0){
					String itemId = JsonParse.itemId(lineTradePile2, x);
					System.out.println("Vendiendo: "+itemId);
					actionHouse(itemId);
				}
			}	
		}catch(Exception e){}
		}

}
